package com.gongpingjialoan.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gongpingjialoan.R;
import com.gongpingjialoan.bean.car.MessageBean;
import com.gongpingjialoan.utility.TimeUtils;

import java.util.List;

/**
 * @Description:
 * @Author: chuang
 * @Since: 2016/9/2 13:51
 * @Version:
 */
public class MessageAdapter extends BaseAdapter {

    private List<MessageBean> mMessageBean;

    private Context mContext;

    private LayoutInflater mLayoutInflater;

    public MessageAdapter(List<MessageBean> assessHistories, Context context) {
        mMessageBean = assessHistories;
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    public void setData(List<MessageBean> mMessageBean) {
        this.mMessageBean = mMessageBean;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return mMessageBean.size();
    }

    @Override
    public Object getItem(int position) {
        return mMessageBean.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.message_item, null);
            holder = new ViewHolder();
            holder.imgThumbnail = (ImageView) convertView.findViewById(R.id.pic);
            holder.tvCarTitle = (TextView) convertView.findViewById(R.id.carName);
            holder.nikeName = (TextView) convertView.findViewById(R.id.nikeName);
            holder.timeTextView = (TextView) convertView.findViewById(R.id.time);
            holder.mileTextView = (TextView) convertView.findViewById(R.id.message_txt);
            holder.msgCount = (TextView) convertView.findViewById(R.id.message_tip);

            convertView.setTag(R.id.tag_first, holder);
        } else {
            holder = (ViewHolder) convertView.getTag(R.id.tag_first);
        }
        MessageBean source = mMessageBean.get(position);
        convertView.setTag(R.id.tag_second, source);
        if(source != null){
            String msg = source.content;
            if(msg != null){
                if(msg.startsWith("txt:\"[")){
                    msg = "[表情]";
                }else if(msg.startsWith("image")){
                    msg = "[图片]";
                }else if(msg.startsWith("location")){
                    msg = "[位置]";
                }else if(msg.startsWith("txt")){
                    msg = msg.substring(5,msg.length()-1).trim();
                }else if(msg.startsWith("voice")){
                    msg = "[语音]";
                }else if(msg.startsWith("video")){
                    msg = "[视频]";
                }else if(msg.startsWith("normal file")){
                    msg = "[文件]";
                }
                holder.mileTextView.setText(msg);
            }
            if(TextUtils.isEmpty(source.model_zh)){
                holder.tvCarTitle.setVisibility(View.GONE);
            }else{
                holder.tvCarTitle.setText(source.model_zh);
            }
            if(TextUtils.isEmpty(source.nickname)){
                holder.nikeName.setText("公平价用户");
            }else{
                holder.nikeName.setText(source.nickname);
            }
            holder.timeTextView.setText(TimeUtils.timeFormat(source.time));
            if(source.msgCount > 0){
                holder.msgCount.setText(source.msgCount +"");
                holder.msgCount.setVisibility(View.VISIBLE);
            }else{
                holder.msgCount.setVisibility(View.GONE);
            }
            Glide.with(mContext).load(source.avatar).placeholder(R.drawable.user_img_icon).into(holder.imgThumbnail);
        }
        return convertView;
    }


    public static class ViewHolder {
        public ImageView imgThumbnail;

        public TextView tvCarTitle;

        public TextView nikeName;

        public TextView timeTextView;

        public TextView msgCount;

        public TextView mileTextView;

    }
}

